import shutil
from pathlib import Path
from .backup import BackupManager

class UbuntuAdapter:
    def __init__(self):
        self.sources_list = Path("/etc/apt/sources.list")
        self.backup = BackupManager("ubuntu")

    def apply_mirror(self, mirror_url: str):
        """应用清华镜像模板"""
        template = f"""
        deb {mirror_url}/ubuntu/ $RELEASE main restricted universe multiverse
        deb {mirror_url}/ubuntu/ $RELEASE-updates main restricted universe multiverse
        """
        self.backup.create()
        self.sources_list.write_text(
            template.replace("$RELEASE", self.detect_codename())
        )

    def detect_codename(self) -> str:
        """获取Ubuntu代号如jammy"""
        return subprocess.getoutput("lsb_release -cs")